import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/entrys/rp/to_review_entity.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/provider/automounting_provider.dart';
import 'package:xiongan_app_yy/provider/autopicking_provider.dart';
import 'package:xiongan_app_yy/provider/toreview_provider.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

//自动拣选——详情列表
class ToreviewDetailList extends StatefulWidget {
  dynamic data;
  bool buttonState=false;

  @override
  State<StatefulWidget> createState() {
    if(data["orderstatetypename"]=="复核完成"){
      buttonState=true;
    }
    return _ToreviewDetailList();
  }

  ToreviewDetailList(this.data);
}

//ListWidget
class _ToreviewDetailList extends State<ToreviewDetailList> {
  List<ToReviewData> _data = List();

  @override
  Widget build(BuildContext context) {
    _data = Provider.of<ToReviewProvider>(context).detailListData.datas;
    return Scaffold(
        appBar: AppBar(
          leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
          backgroundColor: Color(0XFF00A5F1),
          title: Text(
            "复核详情列表",
            style: TextStyle(fontSize: 18),
          ),
          elevation: 0,
          centerTitle: true,
        ),
        body: LoadBuilder(
          visibility: Provider.of<ToReviewProvider>(context).netStatus2,
          builder: Column(
            children: <Widget>[
              Expanded(
                child: ListView.separated(
                    itemBuilder: (BuildContext context, int index) {
                      return TextCartItem(
                        data: _data,
                        index: index,
                        onTapFuns: (int index) {
                          Navigator.of(context).pushNamed("/toreview_detail",arguments: index);
                        },
                      );
                    },
                    separatorBuilder: (BuildContext context, int index) {
                      return Divider(color: Colors.black26);
                    },
                    itemCount: _data.length),
              ),
              Offstage(offstage: widget.buttonState,child: Row(
                children: <Widget>[
                  Expanded(
                      child: FlatButton(
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.all(Radius.circular(0))),
                        padding: EdgeInsets.all(15),
                        color: Color(0xFF42AFF0),
                        child: Text(
                          "提交",
                          style: TextStyle(color: Colors.white),
                        ),
                        onPressed: () {
                          int number = 0;
                          for (int i = 0; i < _data.length; i++) {
                            if (_data.elementAt(i).checkquantity!= 0) {
                              number++;
                            }
                          }
                          if (number == _data.length) {
                            Provider.of<ToReviewProvider>(context)
                                .SubmissionCheckDetail(sendObj: {
                              "id": widget.data["id"],
                              'operaterUserId': DataSingleton().Data.id,
                              'tenantId': DataSingleton().Data.tenantId,
                              'Warehouseid': DataSingleton().warehouseChoice.id,
                              'outCheckDetailList':Provider.of<ToReviewProvider>(context).detailListData.toJson()["datas"],
                            }).then((a){
                              if(a==true){
                                Provider.of<AutoPickingProvider>(context).getList();
                                Navigator.pop(context);
                              }
                            });
                          } else {
                            TostUtil.showToast("请复核完所有");
                          }
                        },
                      ))
                ],
              ))

            ],
          ),
        ));
  }
}

class _Text extends Text {
  TextStyle style;
  _Text(String data,
      {this.style = const TextStyle(color: Color(0xff646464), fontSize: 14)})
      : super(data, style: style);
}

class TextCartItem extends StatelessWidget {
  final List<ToReviewData> data;
  final int index;
  final Function(int index) onTapFuns;

  TextCartItem(
      {Key key, @required this.data, @required this.index, this.onTapFuns})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    Color textColor =
    data.elementAt(index).ischeck== true ? Color(0xFF2FC480) : Color(0xFF00A5F1);
    return GestureDetector(
      child: Container(
          padding: EdgeInsets.all(10.0),
          decoration: BoxDecoration(
              //解决GestureDetector Widget事件触发面问题
              border: Border.all(color: Colors.transparent, width: 1)),
          child: Column(
            children: <Widget>[
              Align(
                alignment: Alignment.bottomLeft,
                child: _Text("物资名称:${data.elementAt(index).materialname??"暂无"}"),
              ),
              Container(
                padding: EdgeInsets.only(top: 5, bottom: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: _Text("下架库区:${data.elementAt(index).areaname??"暂无"}"),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 5, bottom: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: _Text("下架货架:${data.elementAt(index).shelfname??"暂无"}"),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 5, bottom: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: _Text("下架货位:${data.elementAt(index).locationcode??"暂无"}"),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: RichText(
                    text: TextSpan(
                      text: '拣选状态:',
                      style:
                          TextStyle(fontSize: 14.0, color: Color(0xff646464)),
                      children: <TextSpan>[
                        TextSpan(
                          text: data.elementAt(index).ischeck == true ? "已复核" : "未复核",
                          style: TextStyle(fontSize: 14.0, color: textColor),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ],
          )),
      onTap: () {
        if (onTapFuns != null) {
          onTapFuns(index);
        }
      },
    );
  }
}
